package com.server.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.service.ICommonDataService;
import com.server.service.IRewardConfigService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("RewardConfigService")
public class RewardConfigService implements IRewardConfigService {
	
	private static final String TABLE_NAME = "reward.reward_config";
	private static final String VIEW_NAME = "reward.v_reward_config";
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	/**
	 * 返费配置新增
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月7日
	 */
	@Override
	public Boolean add(String operator, Map<String, Object> inputMap) {
		return commonDataService.add(operator, TABLE_NAME, inputMap);
	}

	@Override
	public DataVector<Map<String, Object>> getList(RequestInput requestInput,String orgNo) {
		String whereStr = "OrgNo='"+orgNo+"'";
		String sortStr = "CreateTime desc";
		return commonDataService.getListData(VIEW_NAME, whereStr, sortStr,
				requestInput);
	}
	@Override 
	public List<Map<String, Object>> getList(String orgNo,Date time,Integer nature,Integer sourceType){
		String sql = "select * from %s where Valid=1 and OrgNo='%s' and StartTime<='%s' and EndTime>='%s' %s and SourceTypeId=%s order by CreateTime desc";
		String dateStr = DateUtil.getDateStr(time, DateType.YYYY_MM_DD);
		String realSql = null;
		if (nature.equals(-1)) {
			realSql = String.format(sql, TABLE_NAME,orgNo,dateStr,dateStr,sourceType);
		}else {
			realSql = String.format(sql, TABLE_NAME,orgNo,dateStr,dateStr,"and Nature="+nature,sourceType);
		}
		List<Map<String, Object>> list = jdbcTemplate.queryForList(realSql);
		return list;
	}
	
	@Override 
	public List<Map<String, Object>> getList(String orgNo,String nature,String sourceType,String dateStr){
		dateStr = dateStr.replace("GMT", "").replaceAll("\\(.*\\)", ""); 
		SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z",Locale.ENGLISH); 
		Date dateTrans = new Date();
		try {
			dateTrans = format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		dateStr = new SimpleDateFormat("yyyy-MM-dd").format(dateTrans); 
		String sql = "SELECT * from reward.v_reward_config "
				+ "where Valid=1 and OrgNo='"+orgNo+"' and "
				+"StartTime<='"+dateStr+"' and EndTime>='"+dateStr+"' and "
				+"Nature="+nature+" and SourceTypeId="+sourceType;
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			String startTime = map.get("StartTime").toString().substring(0, 10);
			String endTime = map.get("EndTime").toString().substring(0, 10);
			String typeText = map.get("TypeText").toString();
			String rewardType = map.get("RewardType").toString();
			String totalAmount = map.get("TotalAmount").toString();
			String amount1 = map.get("Amount1").toString();
			String workDay = map.get("WorkDay").toString();
			String limitCount = map.get("LimitCount").toString();
			//String rewardText = startTime + "至" + endTime + typeText + totalAmount;
			String rewardText = "";
			if ( rewardType.equals("1") ) {
				rewardText = "持续返费：在职每满"+workDay+"天，返费"+amount1+"元，最多返费"+limitCount.substring(0, limitCount.length()-2)+"次";
			}else if ( rewardType.equals("3") ) {
				rewardText = "一次性返费：在职满"+workDay+"天，一次性返费"+totalAmount+"元";
			}else if ( rewardType.equals("4") ) {
				rewardText = "按工作日返费：每在职"+workDay+"天，返费"+amount1+"元";
			}else {
				rewardText = startTime + "至" + endTime + typeText + totalAmount;
			}
			
			list.get(i).put("RewardText", rewardText);
		}
		return list;
	}

	@Override
	public Map<String, Object> getForm(String id) {
		Map<String, Object> map = commonDataService.getFormData(VIEW_NAME, "Id", id, null, null);
		if( map!=null && map.size()>0 ){
			String startTime = map.get("StartTime").toString().substring(0, 10);
			String endTime = map.get("EndTime").toString().substring(0, 10);
			String typeText = map.get("TypeText").toString();
			String rewardType = map.get("RewardType").toString();
			String totalAmount = map.get("TotalAmount").toString();
			String amount1 = map.get("Amount1").toString();
			String workDay = map.get("WorkDay").toString();
			String limitCount = map.get("LimitCount").toString();
			//String rewardText = startTime + "至" + endTime + typeText + totalAmount;
			String rewardText = "";
			if ( rewardType.equals("1") ) {
				rewardText = "持续返费：在职每满"+workDay+"天，返费"+amount1+"元，最多返费"+limitCount.substring(0, limitCount.length()-2)+"次";
			}else if ( rewardType.equals("3") ) {
				rewardText = "一次性返费：在职满"+workDay+"天，一次性返费"+totalAmount+"元";
			}else if ( rewardType.equals("4") ) {
				rewardText = "按工作日返费：每在职"+workDay+"天，返费"+amount1+"元";
			}else {
				rewardText = startTime + "至" + endTime + typeText + totalAmount;
			}
			map.put("RewardText", rewardText);
		}
		return map;
	}
	
}
